package com.gjintel.business.webtypemanage.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.business.webtypemanage.model.WebTypeInfo;
import com.gjintel.business.webtypemanage.service.IWebTypeManageService;
import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.user.model.User;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;
import com.gjintel.sdk.dto.TreeDto;
import com.gjintel.sdk.util.StringUtil;

@RequestMapping("/business/webtypemanage")
@Controller
public class WebTypeManageController {

	private static final Logger LOG = Logger.getLogger(WebTypeManageController.class);
	@Autowired
	private IWebTypeManageService webTypeManageService;

	/**
	 * @description 获得输入数据信息
	 * @author fengya
	 * @date 2016-3-15 下午03:55:05
	 * @param id
	 * @return
	 * @return ModelAndView
	 */
	@RequestMapping("/input")
	public ModelAndView input(String id) {
		ModelAndView mv = new ModelAndView("/business/webtypemanage/web-type-input");
		if (!StringUtil.isEmpty(id)) {
			Map<String, Object> webTypeMap = InitCacheData.getInstance().getCacheData(InitCacheData.CacheType.WEB_TYPE);
			WebTypeInfo webTypInfo = (WebTypeInfo) webTypeMap.get(id);
			if (webTypInfo.getPid() != null) {
				Object obj = webTypeMap.get(webTypInfo.getPid());
				if (obj != null) {
					webTypInfo.setPname(((WebTypeInfo) obj).getName());
				}
			}
			mv.addObject("webTypInfo", webTypInfo);
		}
		return mv;
	}

	/**
	 * @description 得的所有的记录数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:22
	 * @return
	 * @return JSONArray
	 */
	@RequestMapping("/listAll")
	@ResponseBody
	public JSONArray listAll() {
		List<WebTypeInfo> list = this.webTypeManageService.findAll();
		return JSONArray.fromObject(list);
	}

	@RequestMapping("/getArticleTreeDialog")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject getArticleTreeDialog(String id, String nodeName, String deleteIds) {
		// 树中节点所在的集合
		List<TreeDto> treeList = new ArrayList<TreeDto>();
		try {
			if (nodeName != null) {
				nodeName = URLDecoder.decode(nodeName, "UTF-8");
			}
		} catch (UnsupportedEncodingException e) {
			LOG.debug("汉字转换错误！", e);
		}
		Map jsonData = new HashMap();
		// 查询到的节点ID的集合
		List<String> selIds = this.webTypeManageService.getWebManageTree(treeList, id, nodeName, deleteIds, true);
		jsonData.put("selIds", JSONArray.fromObject(selIds));
		jsonData.put("tree", JSONArray.fromObject(treeList));
		return JSONObject.fromObject(jsonData);
	}

	/**
	 * @description 获得商品类型树
	 * @author fengya
	 * @date 2016-3-15 下午04:04:10
	 * @param id
	 * @return
	 * @return JSONArray
	 */
	@RequestMapping("/getArticleTypeTree")
	@ResponseBody
	public JSONArray getArticleTypeTree(String id) {
		// 树中节点所在的集合
		List<TreeDto> treeList = new ArrayList<TreeDto>();
		this.webTypeManageService.getWebManageTree(treeList, id, null, null, false);
		return JSONArray.fromObject(treeList);
	}

	/**
	 * @description 分页查询数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:40
	 * @param page
	 * @param goodsType
	 * @param sort
	 * @return
	 * @return JSONObject
	 */
	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, WebTypeInfo articmanage, Sort sort) {
		this.webTypeManageService.findAll(page, articmanage, sort);
		return CommUtil.getPageJSONData(page, true);
	}

	/**
	 * @description 保存数据
	 * @author fengya
	 * @date 2016-3-15 下午03:55:52
	 * @param session
	 * @param goodsType
	 * @return
	 * @return String
	 */
	@RequestMapping("/save")
	@ResponseBody
	public String save(HttpSession session, WebTypeInfo articmanage) {
		User user = (User) session.getAttribute("loginUser");
		if (articmanage != null) {
			if (StringUtil.isEmpty(articmanage.getId())) {
				articmanage.setId(null);
				articmanage.setCreateTime(new Date());
				articmanage.setCreaterName(user.getName());
				articmanage.setCreaterId(user.getId());
			}
			articmanage.setUpdateTime(new Date());
			articmanage.setUpdaterName(user.getName());
			articmanage.setUpdaterId(user.getId());
			this.webTypeManageService.saveOrUpdate(articmanage);
			LogUtil.addLog(new LogInfo("添加文章类型数据", user.getId(), user.getName(), String.valueOf(LogType.ADD.ordinal()), articmanage.getClass().getSimpleName(), articmanage.getId()));
			InitCacheData.getInstance().initWebTypeData();
			return Constant.SAVE_SUCCESS;
		}
		return Constant.SAVE_UN_SUCCESS;
	}

	/**
	 * @description 删除数据
	 * @author fengya
	 * @date 2016-3-15 下午03:56:08
	 * @param deleteIds
	 * @return
	 * @throws Exception
	 * @return String
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(HttpSession session, String deleteIds) throws Exception {
		this.webTypeManageService.removeData(deleteIds);
		User user = (User) session.getAttribute("loginUser");
		LogUtil.addLog(new LogInfo("删除商品类型数据", user.getId(), user.getName(), String.valueOf(LogType.DELETE.ordinal()), WebTypeInfo.class.getSimpleName(), deleteIds));
		InitCacheData.getInstance().initWebTypeData();
		return Constant.DEL_SUCCESS;
	}

}
